package com.zja.dao.Impl;

import com.zja.dao.BaseDAO;
import com.zja.dao.MessageDao;
import com.zja.model.Message;

import java.util.List;

public class MessageDaoImpl extends BaseDAO implements MessageDao {
    @Override
    public List<Message> selectAll() {
        String sql = "SELECT * FROM message";
        try {
            return executeQuery(Message.class, sql, null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Message selectById(Long id) {
        String sql = "SELECT * FROM message WHERE id = ?";
        try {
            return executeQueryBean(Message.class, sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean insert(Message message) {
        String sql = "INSERT INTO message(sender_id, receiver_id, content, created_at) VALUES(?,?,?,?)";
        try {
            return executeUpdate(sql, message.getSenderId(), message.getReceiverId(), message.getContent(), message.getCreatedAt());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean update(Message message) {
        String sql = "UPDATE message SET sender_id = ?, receiver_id = ?, content = ?, created_at = ? WHERE id = ?";
        try {
            return executeUpdate(sql, message.getSenderId(), message.getReceiverId(), message.getContent(), message.getCreatedAt(), message.getId());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public boolean deleteById(Long id) {
        String sql = "DELETE FROM message WHERE id = ?";
        try {
            return executeUpdate(sql, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
